//
//  XLMessageAdapter.m
//  industry
//
//  Created by xl on 2020/11/26.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLMessageAdapter.h"
#import "XLMessageCell.h"
#import "XLMessageModel.h"

@interface XLMessageAdapter ()<MGSwipeTableCellDelegate>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation XLMessageAdapter

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    self.tableView = tableView;
    return self.getAdapterArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 20.0f;
    }
    return 10.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section > self.getAdapterArray.count - 1) {
        return [UITableViewCell new];
    }
    static NSString *cellIdentifiersID = @"XLMessageCell";
    XLMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifiersID];
    if (!cell) {
        cell = [[XLMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifiersID];
    }
    cell.delegate = self;
    id cellData = self.getAdapterArray[indexPath.section];
    XLMessageModel *model = (XLMessageModel *)cellData;
    [cell loadCellData:self.getAdapterArray[indexPath.section] isRead:self.isRead];
    if (tableView.isEditing) {
        cell.selected = model.isSelect;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section > self.getAdapterArray.count - 1) {
        return;
    }
    if (tableView.isEditing) {
        id cellData = self.getAdapterArray[indexPath.section];
        XLMessageModel *model = (XLMessageModel *)cellData;
        model.isSelect = YES;
        return;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (self.adapterDelegate && [self.adapterDelegate respondsToSelector:@selector(didSelectCellData:)]) {
        [self.adapterDelegate didSelectCellData:self.getAdapterArray[indexPath.section]];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.isEditing) {
        id cellData = self.getAdapterArray[indexPath.section];
        XLMessageModel *model = (XLMessageModel *)cellData;
        model.isSelect = NO;
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

#pragma mark - MGSwipeTableCellDelegate
- (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction fromPoint:(CGPoint)point {
    return YES;
}

-(nullable NSArray<UIView*>*) swipeTableCell:(nonnull MGSwipeTableCell*) cell swipeButtonsForDirection:(MGSwipeDirection)direction
                               swipeSettings:(nonnull MGSwipeSettings*) swipeSettings expansionSettings:(nonnull MGSwipeExpansionSettings*) expansionSettings {
    swipeSettings.transition = MGSwipeTransitionBorder;
    if (direction == MGSwipeDirectionRightToLeft) {
        expansionSettings.fillOnTrigger = NO;
        expansionSettings.threshold = 1.1;
        
        WEAKSELF
        MGSwipeButton *deleteBtn = [MGSwipeButton buttonWithTitle:@"" icon:XLIMAGE(@"xxyc") backgroundColor:[UIColor whiteBgColor] callback:^BOOL(MGSwipeTableCell * _Nonnull cell) {
            STRONGSELF
            NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
            if (strongSelf.adapterDelegate && [self.adapterDelegate respondsToSelector:@selector(deleteCellData:indexPath:)]) {
                [strongSelf.adapterDelegate deleteCellData:strongSelf.getAdapterArray[indexPath.section] indexPath:indexPath];
            }
            return YES;
        }];
        return @[deleteBtn];
    }
    return nil;
}
@end
